package com.alibaba.citrus.ots.platform.enhance.functions.diff.sap.processor;

import com.alibaba.citrus.ots.platform.enhance.facade.service.diff.common.DiffCheckRequest;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.constant.DiffStrategyRouteDict;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.router.DiffCheckProcessor;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.common.template.OneDiffOneTemplate;
import com.alibaba.citrus.ots.platform.enhance.functions.diff.sap.constant.ConsignmentOrderLineDiffStandardFieldDict;
import com.alibaba.citrus.ots.platform.enhance.repository.model.diff.common.FieldLineDTO;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import com.alibaba.nacos.common.utils.MapUtils;
import com.epoch.app.otspricecenter.enums.DimensionPropertyEnum;
//import com.epoch.app.otspricecenter.propertyprocess.dto.ObtainInnerCodeAndOuterCodeMapRequest;
import com.epoch.app.otspricecenter.propertyprocess.dto.ObtainInnerCodeAndOuterCodeMapRequest;
import com.epoch.app.otspricecenter.propertyprocess.service.PropertyProcessService;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Objects;

/**
 * @author: qiaojiange
 * @create: 2022/01/24
 * @Description SAP017子单 和 销售子单之间的对账
 */
@Component
public class Sap017SdoDetailDiffConsignmentOrderLineProcessor extends DiffCheckProcessor {

    private Log log = Log.getLogger(Sap017SdoDetailDiffConsignmentOrderLineProcessor.class);
    /**
     * 一对一对账模板
     */
    @Resource
    private OneDiffOneTemplate oneDiffOneTemplate;

    @Override
    public String obtainStrategyRoute() {
        return DiffStrategyRouteDict.SAP017_DETAIL_DIFF_CONSIGNMENT_ORDER_LINE.getStrategyCode();
    }

    @Resource
    private PropertyProcessService propertyProcessService;


    /**
     * @param diffCheckRequest 对账请求
     * @return 对账结果
     * @throws Exception 异常
     */
    @Override
    public Result<String> diff(DiffCheckRequest diffCheckRequest) {
        //这里单独的使用每一个字段进行对账
        Map<String, FieldLineDTO> baseLineDataMap = diffCheckRequest.getBaseLineDataMap();
        //字段转换
        fieldConvert(baseLineDataMap);
        return super.diff(diffCheckRequest,oneDiffOneTemplate);
    }

    /**
     * 字段转换
     *
     * @param baseLineDataMap 基准字段
     */
    private void fieldConvert(Map<String, FieldLineDTO> baseLineDataMap) {
        if (MapUtils.isEmpty(baseLineDataMap)) {
            return;
        }

        //货品编码转换
        FieldLineDTO fieldLineDTO = baseLineDataMap.get(ConsignmentOrderLineDiffStandardFieldDict.SAP_SC_ITEM_ID.getFieldCode());
        if (Objects.isNull(fieldLineDTO)) {
            log.error("转换货品字段行为空！");
            return;
        }

        String outScItemId = fieldLineDTO.getFieldValue();
        if (StringUtils.isNotBlank(outScItemId)) {
            ObtainInnerCodeAndOuterCodeMapRequest scItemIdRequest = new ObtainInnerCodeAndOuterCodeMapRequest();
            scItemIdRequest.setCodeList(Lists.newArrayList(outScItemId));
            scItemIdRequest.setPropertyCode(DimensionPropertyEnum.MATERIAL_CODE.getKey());
            scItemIdRequest.setInnerCode(false);
            Map<String, String> scItemIdMap = (Map<String, String>) propertyProcessService.obtainInnerCodeAndOuterCodeMap(scItemIdRequest);
            String innerScItemId = scItemIdMap.get(outScItemId);
            fieldLineDTO.setFieldValue(innerScItemId);
        }
    }
}
